package stephen.jan20.co12;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserControl {
	public boolean registered(User uf){
	
		boolean result=false;
		Basecontrol ee=new Basecontrol();
		BaseDao u=new BaseDao();
		Connection con=u.getCon();
		String sql="SELECT * FROM user WHERE userName='"+uf.getUserName()+"'";
		try {
			PreparedStatement ps=con.prepareStatement(sql);
			ResultSet rs=ps.executeQuery();
			if(rs.next()){
				System.out.println("注册失败");
			}else{
				ee.add("INSERT INTO user (userName,pwd) VALUES ('"+uf.getUserName()+"','"+uf.getPwd()+"')");
				System.out.println("注册成功");
				result=true;}
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
				
			}
			if(con!=null){
				con.close();
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	public boolean login(User aa){
		boolean result=false;
		BaseDao f=new BaseDao();
		Connection con=f.getCon();
		String sql="SELECT * FROM user WHERE userName='"+aa.getUserName()+"'";
		try {	
		PreparedStatement ps=con.prepareStatement(sql);
		ResultSet rs=ps.executeQuery();
			if(rs.next()){
				try {
					if(rs.getString("pwd").equals(aa.getPwd())){
						result=true;
						System.out.println("登录成功");
					}else{
						System.out.println("登录失败");
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					System.out.println("登录失败");
				}
			}else{
				System.out.println("登录失败");
			}
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
				
			}
			if(con!=null){
				con.close();
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("登录失败");
			
		}
		
		return result;
	}
}
